

########################################################################
# This is a code that is part of the document: Martins, H. C. (2021) Tutorial-articles: the importance of data and code sharing
# This code and the data can be found at https://dataverse.harvard.edu/dataverse/rac

# The purpose of this code is to show how to download open data using R packages that might be useful to Management Scholars.
# Scholars may use these datasets either in tutorial-articles or in innovative empirical research. 

# All remaining errors are author's only. Please send an email to hcm@iag.puc-rio.br if you find any error.

########################################################################


# These are all the packages that we need to load. 
# If you do not have them installed in your computer, please install using the row below:

#install.packages(c("epuR","ggplot2","xts","wbstats","gtrendsR"))

library(epuR)
library(ggplot2)
library(xts)
library(wbstats)
library(gtrendsR)






################### Example 1: Data from the World Bank

catalog<- wbdatacatalog() # Learn all the catalog available

# Let's find one indicator that is one of the Sustainable development goals 
sdg <- wb(country = c("BR"), indicator = 'SI.POV.DDAY', startdate = 2000, enddate = 2020)

# Then we graph.
gSDG <- ggplot(sdg) + 
        geom_point(aes(x=date, y=value)) + 
        geom_line( aes(x=date, y=value) , size=1,group = 1 ) +
        xlab("") + ylab("") + 
        ggtitle("Poverty headcount ratio at $1.90 a day (2011 PPP) (% of population)")+
        theme(plot.title = element_text(color="darkblue", size=14, face="bold"),
            axis.title.x = element_text(size=10, face="bold"),
            axis.title.y = element_text(size=10, face="bold"),
            axis.text.y = element_text(color = "darkblue", size = 10),
            axis.text.x = element_text(color = "darkblue", size = 10),
            panel.background = element_rect(fill = "white", colour = "white"))

ggsave("Example_1.png", width = 20, height = 10, units = "cm")








################### Example 2: Google Trends

geo<- "BR"
time <- "2010-01-01 2020-07-31" # define the period you want. Use one of the following definitions:
# "now 1-H" Last hour, "now 4-H" Last four hours, "now 1-d" Last day, "now 7-d" Last seven days, "today 1-m" Past 30 days, "today 3-m" Past 90 days
# "today 12-m" Past 12 months, "today+5-y" Last five years (default), "all" Since 2004, "Y-m-d Y-m-d" Time span between two dates 

trends1<-gtrends(keyword = "Administra��o",geo = geo,time = time , gprop = c("web"),category = 0,hl = "en-US",low_search_volume = FALSE,cookie_url = "http://trends.google.com/Cookies/NID",tz = 0,onlyInterest = FALSE)
x1     <-trends1$interest_over_time$date 
y1     <-trends1$interest_over_time$hits

trends2<-gtrends(keyword = "Curso t�cnico",geo = geo,time = time,gprop = c("web"),category = 0,hl = "en-US",low_search_volume = FALSE,cookie_url = "http://trends.google.com/Cookies/NID",tz = 0,onlyInterest = FALSE)
x2     <-trends2$interest_over_time$date 
y2     <-trends2$interest_over_time$hits

trends3<-gtrends(keyword = "Gradua��o",geo = geo,time = time ,gprop = c("web"),category = 0,hl = "en-US",low_search_volume = FALSE,cookie_url = "http://trends.google.com/Cookies/NID",tz = 0,onlyInterest = FALSE)
x3     <-trends3$interest_over_time$date 
y3     <-trends3$interest_over_time$hits

trends4<-gtrends(keyword = "MBA",geo = geo,time = time,gprop = c("web"),category = 0,hl = "en-US",low_search_volume = FALSE,cookie_url = "http://trends.google.com/Cookies/NID",tz = 0,onlyInterest = FALSE)
x4     <-trends4$interest_over_time$date 
y4     <-trends4$interest_over_time$hits

trends5<-gtrends(keyword = "Mestrado",geo = geo,time = time,gprop = c("web"),category = 0,hl = "en-US",low_search_volume = FALSE,cookie_url = "http://trends.google.com/Cookies/NID",tz = 0,onlyInterest = FALSE)
x5     <-trends5$interest_over_time$date 
y5     <-trends5$interest_over_time$hits

trends6<-gtrends(keyword = "Doutorado",geo = geo,time = time,gprop = c("web"),category = 0,hl = "en-US",low_search_volume = FALSE,cookie_url = "http://trends.google.com/Cookies/NID",tz = 0,onlyInterest = FALSE)
x6     <-trends6$interest_over_time$date 
y6     <-trends6$interest_over_time$hits

# In the US
geo<- "US"
trends1US<-gtrends(keyword = "Management",geo = geo,time = time ,gprop = c("web"),category = 0,hl = "en-US",low_search_volume = FALSE,cookie_url = "http://trends.google.com/Cookies/NID",tz = 0,onlyInterest = FALSE)
x1US     <-trends1US$interest_over_time$date 
y1US     <-trends1US$interest_over_time$hits


trends2US<-gtrends(keyword = "MBA",geo = geo,time = time ,gprop = c("web"),category = 0,hl = "en-US",low_search_volume = FALSE,cookie_url = "http://trends.google.com/Cookies/NID",tz = 0,onlyInterest = FALSE)
x2US     <-trends2US$interest_over_time$date 
y2US     <-trends2US$interest_over_time$hits

trends3US<-gtrends(keyword = "Master",geo = geo,time = time ,gprop = c("web"),category = 0,hl = "en-US",low_search_volume = FALSE,cookie_url = "http://trends.google.com/Cookies/NID",tz = 0,onlyInterest = FALSE)
x3US     <-trends3US$interest_over_time$date 
y3US     <-trends3US$interest_over_time$hits

trends4US<-gtrends(keyword = "Phd",geo = geo,time = time ,gprop = c("web"),category = 0,hl = "en-US",low_search_volume = FALSE,cookie_url = "http://trends.google.com/Cookies/NID",tz = 0,onlyInterest = FALSE)
x4US     <-trends4US$interest_over_time$date 
y4US     <-trends4US$interest_over_time$hits


# Ploting graphs together
png("Example_2A.png", width = 1500, height = 1000)
par(mfrow=c(3,2))
plot(y1~x1,type = "b",lty = 1,main = "Administra��o (in Brazil)",xlab = "",ylab = "",col="darkblue",cex.main=3, cex.axis=2.7)
plot(y2~x2,type = "b",lty = 1,main = "Curso t�cnico (in Brazil)",xlab = "",ylab = "", col="darkgreen", cex.main=3, cex.axis=2.7)
plot(y3~x3,type = "b",lty = 1,main = "Gradua��o (in Brazil)",xlab = "",ylab = "", col="darkviolet", cex.main=3, cex.axis=2.7)
plot(y4~x4,type = "b",lty = 1,main = "MBA (in Brazil)",xlab = "",ylab = "", col="darkorange", cex.main=3, cex.axis=2.7)
plot(y5~x5,type = "b",lty = 1,main = "Mestrado (in Brazil)",xlab = "",ylab = "", col="brown", cex.main=3, cex.axis=2.7)
plot(y6~x6,type = "b",lty = 1,main = "Doutorado (in Brazil)",xlab = "",ylab = "", col="red", cex.main=3, cex.axis=2.7)
dev.off()


png("Example_2B.png", width = 1500, height = 1000)
par(mfrow=c(2,2))
plot(y1US~x1US,type = "b",lty = 1,main = "Management (in the US)",xlab = "",ylab = "",col="darkblue", cex.main=3, cex.axis=2.7)
plot(y2US~x2US,type = "b",lty = 1,main = "MBA (in the US)",xlab = "",ylab = "", col="darkgreen", cex.main=3, cex.axis=2.7)
plot(y3US~x3US,type = "b",lty = 1,main = "Master (in the US)",xlab = "",ylab = "", col="darkviolet", cex.main=3, cex.axis=2.7)
plot(y4US~x4US,type = "b",lty = 1,main = "Phd (in the US)",xlab = "",ylab = "", col="darkorange",cex.main=3, cex.axis=2.7)
dev.off()



















################### Example 3: Data from the Economic Policy Uncertainty dataset
################### https://www.policyuncertainty.com/ 

# Download Economic Policy Uncertainty data
epu_brazil      <- get_EPU(region = "Brazil") # There are other countries, you may change this function.

# At the moment I am using, the data go until Fev 2020. Let's include the more recent values for Brazil. 
# I found this values in the original EPU website.

data  <- c(368.8998413,312.1482849,308.7680969,116.8758011)
dates <- seq(as.Date("2020-03-01"), length=4, by="month")
add   <- xts(x=data, order.by=dates)
epu_brazil <- rbind(epu_brazil,add) # Appending data 


# Downloading World Uncertainty Index
wui_overall <- get_WUI("F1")

# Downloading Geopolitical Risk Index
gpr_all     <- get_GPR(type = 3)
gpr_brazil  <- gpr_all$GPR_BRAZIL

# Including the three graphs together EPU, WUI and GPR:
png(file="Example_3.png",width=1000, height=700)
par(mfrow = c(3, 1))
plot(epu_brazil, main="Economic Policy Uncertainty",cex.axis=1.5)
plot(wui_overall, main="World Uncertainty Index", cex.axis=1.5)
plot(gpr_brazil, main="Geopolitical Risk Index", cex.axis=1.5)
dev.off()




